package org.swidgets.base.sinputbox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TextBoxBase;

public class SDecimalInputBox extends SRegexInputBox {
	private int numberDigit = 0;
	
	private static final boolean isDesignTime() {
		return false;
	}
	
	public SDecimalInputBox() {
		super(RegExpConstants.DECIMAL, RegExpConstants.DECIMAL_ERROR_MESSAGE);
		this.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
		if (isDesignTime()) {
			this.setText("DecimalInputBox");
		}
	}

	public SDecimalInputBox(String label) {
		super(label,RegExpConstants.DECIMAL, RegExpConstants.DECIMAL_ERROR_MESSAGE);
		this.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
	}
	
	public SDecimalInputBox(int numberDigit) {
		super(RegExpConstants.DECIMAL,  RegExpConstants.DECIMAL_ERROR_MESSAGE);
		this.numberDigit = numberDigit;
		this.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
	}

	public SDecimalInputBox(String label,int numberDigit) {
		super(label, RegExpConstants.DECIMAL,  RegExpConstants.DECIMAL_ERROR_MESSAGE);
		this.numberDigit = numberDigit;
		this.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
	}
	
	
	protected void onFailure() {
		GWT.log("NumericTextBox.onFailure --> Formato Errato", null);
		errorOn(this.message + " - Accettati numeri con " + numberDigit + " decimali");
		
	}

	protected boolean vetoChange() {
		String testo = this.getText();
		if(testo != null && testo.length() > 0) {
			if (testo.indexOf(",")>0) {
				String numero[] = testo.split(",");
				if(numero[1] != null && numero[1].length() > numberDigit) {
					return false;
				}
			}
		}
		
		return true;
	}

	public int getNumberDigit() {
		return numberDigit;
	}

	public void setNumberDigit(int numberDigit) {
		this.numberDigit = numberDigit;
	}
}